package com.toe.user.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.pojo.domain.ToeAuthenticationInfo;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.StateEnum;
import com.toe.pojo.dto.user.UserAddressDTO;
import com.toe.pojo.entity.user.UserAddress;
import com.toe.pojo.vo.user.UserAddressVO;
import com.toe.user.webapi.mapper.UserAddressMapper;
import com.toe.user.service.UserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public void insert(UserAddressDTO userAddressDTO) {
        log.info("开始新增用户的地址的业务!!!");
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        ToeAuthenticationInfo authenticationInfo = (ToeAuthenticationInfo)credentials;
        Long userId = authenticationInfo.getId();
        UserAddress address = userAddressMapper.selectAddress(userId, userAddressDTO.getAddress());
        if (address!=null){
            throw new ToeServiceException(StateEnum.ERR_CLIENT,"请勿重复添加!!!");
        }
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userAddressDTO,userAddress);
        log.info("开始绑定时间");
        LocalDateTime date = LocalDateTime.now();
        userAddress.setCreateTime(date);
        userAddress.setUpdateTime(date);
        userAddress.setUserId(userId);
        userAddressMapper.insert(userAddress);
    }

    @Override
    public void deleteById(long id) {
        log.info("开始删除用户的地址的业务!!!");
        userAddressMapper.deleteById(id);
    }

    @Override
    public void updateAddress(long id, String address) {
        log.info("开始修改用户的地址的业务!!!");
        UserAddress userAddress = userAddressMapper.selectById(id);
        UserAddress address1 = userAddressMapper.selectAddress(userAddress.getUserId(), address);
        if (address1!=null){
            throw new ToeServiceException(StateEnum.ERR_CLIENT,"请勿重复添加!!!");
        }
        userAddressMapper.updateAddress(id, address);
        log.info("业务完成!!!!");
    }


    @Override
    public List<UserAddressVO> selectUserAddress() {
        log.info("开始查询用户的地址的业务!!!");
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        ToeAuthenticationInfo authenticationInfo = (ToeAuthenticationInfo)credentials;
        Long userId = authenticationInfo.getId();
        List<UserAddressVO> userAddressVOS = userAddressMapper.selectUserAddress(userId);
        return userAddressVOS;
    }

    @Override
    public JsonPage<UserAddressVO> selectUserAddressPage(Integer pageNum, Integer pageSize) {
        log.info("开始查询用户的地址的业务!!!");
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        ToeAuthenticationInfo authenticationInfo = (ToeAuthenticationInfo)credentials;
        Long userId = authenticationInfo.getId();
        PageHelper.startPage(pageNum,pageSize);
        List<UserAddressVO> userAddressVOS = userAddressMapper.selectUserAddress(userId);
        return JsonPage.restPage(new PageInfo<>(userAddressVOS));
    }


}
